package com.excesys.exsecs.eco.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.excesys.exsecs.eco.entity.EcoWeatherFlow;
import com.excesys.exsecs.eco.service.EcoWeatherFlowService;
import com.excesys.exsecs.eco.mapper.EcoWeatherFlowMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 天气流水表
 *
 * @author lxc
 * @date 2024-11-29 10:07:03
 */
@Service
@RequiredArgsConstructor
public class EcoWeatherFlowServiceImpl extends ServiceImpl<EcoWeatherFlowMapper, EcoWeatherFlow> implements EcoWeatherFlowService {

	private final EcoWeatherFlowMapper weatherFlowMapper;
	@Override
	public List getMaxChart(String startDate, String endDate) {

		if (startDate.matches("\\d{4}-\\d{2}-\\d{2}") && endDate.matches("\\d{4}-\\d{2}-\\d{2}")) {
			List<EcoWeatherFlow> ecoWeatherFlows = weatherFlowMapper.selectList(Wrappers.<EcoWeatherFlow>lambdaQuery()
					.between(EcoWeatherFlow::getTimeValue, startDate, endDate));
			return ecoWeatherFlows.stream().map(it ->{
				Map map = new HashMap<>();
				map.put("timeValue",it.getTimeValue());
				map.put("maxValue",it.getMaxValue());
				return map;
			}).collect(Collectors.toList());
		}
		else if (startDate.matches("^\\d{4}-(0[1-9]|1[0-2])$") && endDate.matches("^\\d{4}-(0[1-9]|1[0-2])$")) {
			return weatherFlowMapper.selectMax("%Y-%m",startDate+"-01",endDate+"-30");
		}
		else{
			return weatherFlowMapper.selectMax("%Y",startDate+"-01-01",endDate+"-12-31");
		}
	}
}